﻿using LiveChartsCore.SkiaSharpView;
using LiveChartsCore;
using RS.ZhaoXi.Models;
using RS.ZhaoXiWidgets.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using LiveChartsCore.SkiaSharpView.Painting;
using SkiaSharp;
using LiveChartsCore.SkiaSharpView.WPF;
using LiveChartsCore.SkiaSharpView.Drawing.Geometries;
using LiveChartsCore.SkiaSharpView.VisualElements;
using LiveChartsCore.Drawing;
using LiveChartsCore.SkiaSharpView.Extensions;
using System.Reflection;
using System.Xml.Linq;
using LiveChartsCore.Kernel.Sketches;


namespace RS.ZhaoXi.Views.Home
{
    public class HomeViewModel : NotifyBase
    {

        public HomeViewModel()
        {
            //    var value1 = new ChartValues<double>();
            //    var value2 = new ChartValues<double>();

            var value1 = new List<double>();
            var value2 = new List<double>();
            List<string> labelList = new List<string>();
            for (int i = 8; i <= 18; i++)
            {
                labelList.Add($"{i}:00");
                value1.Add(new Random(Guid.NewGuid().GetHashCode()).Next(100, 300));
                value2.Add(new Random(Guid.NewGuid().GetHashCode()).Next(100, 300));
            }


            #region 版本1的绑定
            //这是版本1的绑定
            //    // 初始化数据
            //    BarSeriesCollection = new SeriesCollection
            //    {
            //        new ColumnSeries
            //        {
            //            Title = "生产计数",
            //            Values = value1,
            //            Fill = new LinearGradientBrush
            //            {
            //                GradientStops = new GradientStopCollection
            //                {
            //                    new GradientStop((Color)ColorConverter.ConvertFromString("#2DEBEA"), 0),
            //                    new GradientStop((Color)ColorConverter.ConvertFromString("#49bae0"), 1)
            //                }
            //            }
            //        },
            //        new ColumnSeries
            //        {
            //            Title = "不良技术",
            //            Values = value2,
            //             Fill = new LinearGradientBrush
            //            {
            //                GradientStops = new GradientStopCollection
            //                {
            //                    new GradientStop((Color)ColorConverter.ConvertFromString("#6b4248"), 0),
            //                    new GradientStop((Color)ColorConverter.ConvertFromString("#ed9088"), 1)
            //                }
            //            }
            //        }
            //    };

            //    // 初始化数据
            //    LineSeriesCollection = new SeriesCollection
            //{
            //    new LineSeries
            //    {
            //        Values = value1,
            //        Fill = new LinearGradientBrush
            //        {
            //            StartPoint=new System.Windows.Point(0.5,0),
            //                EndPoint=new System.Windows.Point(0.5,1),
            //            GradientStops = new GradientStopCollection
            //            {
            //                new GradientStop((Color)ColorConverter.ConvertFromString("#2DEBEA"), 0),
            //                new GradientStop(Colors.Transparent, 1)
            //            }
            //        },

            //         PointGeometry = null,

            //    },
            //};


            //    Labels = labelList.ToArray();
            //    PieSeriesCollection = new SeriesCollection();
            //    for (int i = 0; i < 13; i++)
            //    {
            //        var pieSeries = new PieSeries
            //        {
            //            Title = "压差",
            //            Values = new ChartValues<double> { new Random(Guid.NewGuid().GetHashCode()).Next(10, 50) },
            //            DataLabels = true,
            //            LabelPoint = chartPoint =>
            //            {
            //                return $"{chartPoint.SeriesView.Title} {chartPoint.Y}";
            //            },
            //            Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#55ffffff")),
            //            FontSize = 11,
            //            StrokeThickness = 0,
            //            LabelPosition = PieLabelPosition.OutsideSlice
            //        };
            //        PieSeriesCollection.Add(pieSeries);
            //    }
            #endregion

            this.BarSeries = new ISeries[] {
                new ColumnSeries<double> {
                        Name = "生产计数",
                        Values = value1,
                        Stroke = null,
                        Rx = 0,
                        Ry = 0,
                        Fill = new LinearGradientPaint(
                        new SKColor[] { SKColor.Parse("#2DEBEA"), SKColor.Parse("#49bae0") }, // 渐变的颜色
                        new SKPoint(0.5f, 0), // 渐变起始点
                        new SKPoint(0.5f, 1)) // 渐变终点
                        },
                new ColumnSeries<double>
                {
                Name = "不良技术",
                Values =value2,
                Stroke = null,
                Rx = 0,
                Ry = 0,

                Fill = new LinearGradientPaint(
                new SKColor[] { SKColor.Parse("#E969ED"), SKColors.Transparent }, // 渐变的颜色
                new SKPoint(0.5f, 0), // 渐变起始点
                new SKPoint(0.5f, 1)),// 渐变终点
                }};

            this.BarXAxes = new Axis[] {
            new Axis {
            Labels = labelList.ToArray(),
            LabelsRotation = 0,
            //SeparatorsPaint = new SolidColorPaint(new SKColor(200, 200, 200)),
            SeparatorsAtCenter = false,
            TicksPaint = new SolidColorPaint(new SKColor(35, 35, 35)),
            TicksAtCenter = true,
             //By default the axis tries to optimize the number of // mark
             //labels to fit the available space, // mark
             //when you need to force the axis to show all the labels then you must: // mark
            ForceStepToMin = true, // mark
            MinStep = 1,// mark
            TextSize=11,
             LabelsPaint = new SolidColorPaint(SKColor.Parse("#55ffffff")),
            }};


            this.BarYAxes = new Axis[] {
                  new Axis {
            //LabelsRotation = 0,
            SeparatorsPaint = new SolidColorPaint(SKColor.Parse("#55FFFFFF")),
            SeparatorsAtCenter = false,
            TicksPaint = new SolidColorPaint(new SKColor(35, 35, 35)),
            TicksAtCenter = true,
          ForceStepToMin = true, // mark
            MinStep = 50,// mark
            TextSize=11,
             LabelsPaint = new SolidColorPaint(SKColor.Parse("#55ffffff")),
            }

           };

            #region 折线图

            this.LineXAxes = new Axis[] {
            new Axis {
            Labels = labelList.ToArray(),
            LabelsRotation = 0,
            //SeparatorsPaint = new SolidColorPaint(new SKColor(200, 200, 200)),
            SeparatorsAtCenter = false,
            TicksPaint = new SolidColorPaint(new SKColor(35, 35, 35)),
            TicksAtCenter = true,
             //By default the axis tries to optimize the number of // mark
             //labels to fit the available space, // mark
             //when you need to force the axis to show all the labels then you must: // mark
            ForceStepToMin = true, // mark
            MinStep = 1,// mark
            TextSize=11,
             LabelsPaint = new SolidColorPaint(SKColor.Parse("#55ffffff")),
            }};


            this.LineTitle =
            new LabelVisual
            {
                Text = "My chart title",
                TextSize = 25,
                Padding = new LiveChartsCore.Drawing.Padding(15)
            };

            this.LineSeries = new ISeries[] {
         new LineSeries<double>
        {
            Values =value1,
            GeometryStroke = null,
            GeometryFill = null,
            GeometrySize = 0,
            DataPadding = new LvcPoint(0,0),
             Stroke = new SolidColorPaint(SKColor.Parse("#489ca6"), 1),
             Fill = new LinearGradientPaint(
                        new SKColor[] { SKColor.Parse("#489ca6"), SKColors.Transparent }, // 渐变的颜色从蓝色到红色
                        new SKPoint(0.5f, 0), // 渐变起始点
                        new SKPoint(0.5f, 0.5f)) // 渐变终点
                }
            };



            this.LineYAxes = new Axis[] {
                  new Axis {
            //LabelsRotation = 0,
            SeparatorsPaint = new SolidColorPaint(SKColor.Parse("#55FFFFFF")),
            SeparatorsAtCenter = false,
            TicksPaint = new SolidColorPaint(new SKColor(35, 35, 35)),
            TicksAtCenter = true,
          ForceStepToMin = true, // mark
            MinStep = 50,// mark
            TextSize=11,
             LabelsPaint = new SolidColorPaint(SKColor.Parse("#55ffffff")),
            }
            };
            #endregion


            #region Pie图

            //  this.PieSeries = new ISeries[]
            //{
            //          new PieSeries<double>
            //          {
            //               MaxRadialColumnWidth = 200,
            //              Values = new double[] { 10 },
            //              Name = "部分 1",
            //              InnerRadius = 10 // 设置内半径大小，调整这个值来改变环的宽度
            //          },
            //          new PieSeries<double>
            //          {
            //              Values = new double[] { 20 },
            //              Name = "部分 2",
            //              InnerRadius = 10,

            //          },
            //          new PieSeries<double>
            //          {
            //              Values = new double[] { 30 },
            //              Name = "部分 3",
            //              InnerRadius = 10,
            //          }
            //};




            string[] _names = ["压差", "压差", "压差", "压差", "压差", "压差", "压差", "压差", "压差", "压差", "压差", "压差", "压差"];
            List<double> pieData = new List<double>();
            for (int i = 0; i < _names.Count(); i++)
            {
                pieData.Add(new Random(Guid.NewGuid().GetHashCode()).Next(10, 50));
            }

            int _index = 0;
            this.PieSeries = pieData.AsPieSeries((value, series) =>
              {
                  series.InnerRadius = 60;
                  series.Name = _names[_index];
                  series.DataLabelsPosition = LiveChartsCore.Measure.PolarLabelsPosition.Outer; // mark
                  series.DataLabelsSize = 11;
                  series.DataLabelsPaint = new SolidColorPaint(SKColor.Parse("#55ffffff"));
                  series.DataLabelsFormatter =
                     point =>
                     {
                         //  $"This slide takes {point.Coordinate.PrimaryValue} " +
                         //$"out of {point.StackedValue!.Total} parts";
                         return $"{point.Context.Series.Name} {point.Context.DataSource}";
                     };
                  series.ToolTipLabelFormatter = point => $"{point.StackedValue!.Share:P2}";
                  _index++;
              });
            #endregion

            #region 雷达图

            this.RadialSeries = new ISeries[] {


        new PolarLineSeries<int>
        {
            Values = [7, 5, 7, 5, 6,5, 6],
            GeometrySize= 0,
             Stroke = new SolidColorPaint(SKColor.Parse("#E969ED"), 1),
            Fill = new SolidColorPaint(SKColors.Transparent)
        },
        new PolarLineSeries<int>
        {
            Values = [2, 7, 5, 9, 7,9, 7],
            GeometrySize = 0,
            Fill = new SolidColorPaint(SKColors.Transparent),
            Stroke = new SolidColorPaint(SKColor.Parse("#489ca6"), 1),
        }
    };

            this.AngleAxes = [
                 new PolarAxis
        {
            LabelsRotation = LiveCharts.TangentAngle,
            Labels = ["Tag1", "Tag2", "Tag3", "Tag4", "Tag5","Tag6", "Tag7"],
            TextSize = 11,
            LabelsBackground=LvcColor.Empty,
          LabelsPaint = new SolidColorPaint(SKColor.Parse("#55ffffff")),

        }
             ];

            this.RadialAxes = [
                 new PolarAxis
        {
          MinStep=5,
          ForceStepToMin=true,
          SeparatorsPaint=new SolidColorPaint(SKColor.Parse("#55ffffff")),
          LabelsPaint=new SolidColorPaint(SKColor.Parse("#55ffffff")),
          TextSize=11,
          LabelsBackground=LvcColor.Empty,
        }
             ];



            #endregion

        }

        //      public PolarAxis[] RadialAxes { get; set; } =
        //[
        //    new PolarAxis
        //      {
        //          LabelsAngle = -60,
        //          MaxLimit = 30 // null to let the chart autoscale (defualt is null) // mark
        //      }
        //];

        private PolarAxis[] radialAxes;

        public PolarAxis[] RadialAxes
        {
            get { return radialAxes; }
            set
            {
                radialAxes = value;
                this.OnPropertyChanged();
            }
        }


        private PolarAxis[] angleAxes;

        public PolarAxis[] AngleAxes
        {
            get { return angleAxes; }
            set
            {
                angleAxes = value;
                this.OnPropertyChanged();
            }
        }


        private ISeries[] radialSeries;

        public ISeries[] RadialSeries
        {
            get { return radialSeries; }
            set
            {
                radialSeries = value;
                this.OnPropertyChanged();
            }
        }


        private ISeries[] barSeries;

        public ISeries[] BarSeries
        {
            get { return barSeries; }
            set
            {
                barSeries = value;
                this.OnPropertyChanged();
            }
        }


        private Axis[] barXAxes;

        public Axis[] BarXAxes
        {
            get { return barXAxes; }
            set
            {
                barXAxes = value;
                this.OnPropertyChanged();
            }
        }


        private Axis[] barYAxes;

        public Axis[] BarYAxes
        {
            get { return barYAxes; }
            set
            {
                barYAxes = value;
                this.OnPropertyChanged();
            }
        }


        private ISeries[] lineSeries;

        public ISeries[] LineSeries
        {
            get { return lineSeries; }
            set
            {
                lineSeries = value;
                this.OnPropertyChanged();
            }
        }

        private Axis[] lineXAxes;

        public Axis[] LineXAxes
        {
            get { return lineXAxes; }
            set
            {
                lineXAxes = value;
                this.OnPropertyChanged();
            }
        }


        private Axis[] lineYAxes;

        public Axis[] LineYAxes
        {
            get { return lineYAxes; }
            set
            {
                lineYAxes = value;
                this.OnPropertyChanged();
            }
        }


        private LabelVisual lineTitle;

        public LabelVisual LineTitle
        {
            get { return lineTitle; }
            set
            {
                lineTitle = value;
                this.OnPropertyChanged();
            }
        }




        private IEnumerable<ISeries> pieSeries;

        public IEnumerable<ISeries> PieSeries
        {
            get { return pieSeries; }
            set
            {
                pieSeries = value;
                this.OnPropertyChanged();
            }
        }





        #region 版本1的绑定



        //private SeriesCollection pieSeriesCollection;

        //public SeriesCollection PieSeriesCollection
        //{
        //    get { return pieSeriesCollection; }
        //    set
        //    {
        //        pieSeriesCollection = value;
        //        this.OnPropertyChanged();
        //    }
        //}

        //private SeriesCollection barSeriesCollection;

        //public SeriesCollection BarSeriesCollection
        //{
        //    get { return barSeriesCollection; }
        //    set
        //    {
        //        barSeriesCollection = value;
        //        this.OnPropertyChanged();
        //    }
        //}

        //private SeriesCollection lineSeriesCollection;

        //public SeriesCollection LineSeriesCollection
        //{
        //    get { return lineSeriesCollection; }
        //    set
        //    {
        //        lineSeriesCollection = value;
        //        this.OnPropertyChanged();
        //    }
        //}

        #endregion

        private string[] labels;

        public string[] Labels
        {
            get { return labels; }
            set
            {
                labels = value;
                this.OnPropertyChanged();
            }
        }




        private ObservableCollection<DataModel> enviromentDataList;

        public ObservableCollection<DataModel> EnviromentDataList
        {
            get
            {
                if (enviromentDataList == null)
                {
                    enviromentDataList = new ObservableCollection<DataModel>();
                    enviromentDataList.Add(new DataModel()
                    {
                        Data = 123,
                        DataDes = "光照(Lux)"
                    });
                    enviromentDataList.Add(new DataModel()
                    {
                        Data = 11,
                        DataDes = "噪音(db)"
                    });
                    enviromentDataList.Add(new DataModel()
                    {
                        Data = 22,
                        DataDes = "温度(℃)"
                    });

                    enviromentDataList.Add(new DataModel()
                    {
                        Data = 0,
                        DataDes = "湿度(%)"
                    });

                    enviromentDataList.Add(new DataModel()
                    {
                        Data = 0,
                        DataDes = "PM2.5(m³)"
                    });

                    enviromentDataList.Add(new DataModel()
                    {
                        Data = 0,
                        DataDes = "硫化氢(ppm)"
                    });

                    enviromentDataList.Add(new DataModel()
                    {
                        Data = 0,
                        DataDes = "氮气(ppm)"
                    });
                }
                return enviromentDataList;
            }
            set
            {
                enviromentDataList = value;
                this.OnPropertyChanged();
            }
        }

        private ObservableCollection<DataModel> deviceDataList;

        public ObservableCollection<DataModel> DeviceDataList
        {
            get
            {
                if (deviceDataList == null)
                {
                    deviceDataList = new ObservableCollection<DataModel>();

                    deviceDataList.Add(new DataModel()
                    {
                        Data = 311.2,
                        DataDes = "电能(kw.h)"
                    });
                    deviceDataList.Add(new DataModel()
                    {
                        Data = 311.2,
                        DataDes = "电压(V)"
                    });
                    deviceDataList.Add(new DataModel()
                    {
                        Data = 311.2,
                        DataDes = "电流(A)"
                    });

                    deviceDataList.Add(new DataModel()
                    {
                        Data = 311.2,
                        DataDes = "压差(kpa)"
                    });

                    deviceDataList.Add(new DataModel()
                    {
                        Data = 311.2,
                        DataDes = "温度(℃)"
                    });

                    deviceDataList.Add(new DataModel()
                    {
                        Data = 311.2,
                        DataDes = "振动(mm/s)"
                    });

                    deviceDataList.Add(new DataModel()
                    {
                        Data = 311.2,
                        DataDes = "转速(r/min)"
                    });
                    deviceDataList.Add(new DataModel()
                    {
                        Data = 311.2,
                        DataDes = "气压(kpa)"
                    });
                }
                return deviceDataList;
            }
            set
            {
                deviceDataList = value;
                this.OnPropertyChanged();
            }
        }


        private ObservableCollection<AlertModel> alertModelList;

        public ObservableCollection<AlertModel> AlertModelList
        {
            get
            {
                if (alertModelList == null)
                {
                    alertModelList = new ObservableCollection<AlertModel>();

                    alertModelList.Add(new AlertModel()
                    {
                        Number = "1",
                        AlertContent = "设备温度过高",
                        AlertTime = DateTime.Now,
                        AlertDuration = 5
                    });

                    alertModelList.Add(new AlertModel()
                    {
                        Number = "2",
                        AlertContent = "车间温度过高",
                        AlertTime = DateTime.Now,
                        AlertDuration = 5
                    });

                    alertModelList.Add(new AlertModel()
                    {
                        Number = "3",
                        AlertContent = "设备转速过高",
                        AlertTime = DateTime.Now,
                        AlertDuration = 5
                    });

                    alertModelList.Add(new AlertModel()
                    {
                        Number = "1",
                        AlertContent = "设备温度过高",
                        AlertTime = DateTime.Now,
                        AlertDuration = 5
                    });

                    alertModelList.Add(new AlertModel()
                    {
                        Number = "2",
                        AlertContent = "车间温度过高",
                        AlertTime = DateTime.Now,
                        AlertDuration = 5
                    });

                    alertModelList.Add(new AlertModel()
                    {
                        Number = "3",
                        AlertContent = "设备转速过高",
                        AlertTime = DateTime.Now,
                        AlertDuration = 5
                    });


                    alertModelList.Add(new AlertModel()
                    {
                        Number = "1",
                        AlertContent = "设备温度过高",
                        AlertTime = DateTime.Now,
                        AlertDuration = 5
                    });

                    alertModelList.Add(new AlertModel()
                    {
                        Number = "2",
                        AlertContent = "车间温度过高",
                        AlertTime = DateTime.Now,
                        AlertDuration = 5
                    });

                    alertModelList.Add(new AlertModel()
                    {
                        Number = "3",
                        AlertContent = "设备转速过高",
                        AlertTime = DateTime.Now,
                        AlertDuration = 5
                    });


                    alertModelList.Add(new AlertModel()
                    {
                        Number = "1",
                        AlertContent = "设备温度过高",
                        AlertTime = DateTime.Now,
                        AlertDuration = 5
                    });

                    alertModelList.Add(new AlertModel()
                    {
                        Number = "2",
                        AlertContent = "车间温度过高",
                        AlertTime = DateTime.Now,
                        AlertDuration = 5
                    });

                    alertModelList.Add(new AlertModel()
                    {
                        Number = "3",
                        AlertContent = "设备转速过高",
                        AlertTime = DateTime.Now,
                        AlertDuration = 5
                    });

                }
                return alertModelList;
            }
            set
            {
                alertModelList = value;
                this.OnPropertyChanged();
            }
        }




        private ObservableCollection<AttendanceSum> attendanceSumList;

        public ObservableCollection<AttendanceSum> AttendanceSumList
        {
            get
            {
                if (attendanceSumList == null)
                {
                    attendanceSumList = new ObservableCollection<AttendanceSum>();

                    attendanceSumList.Add(new AttendanceSum()
                    {
                        Name="空城之夏",
                        Position="技术总监",
                        WorkDays=128,
                    });


                    attendanceSumList.Add(new AttendanceSum()
                    {
                        Name = "坏笑",
                        Position = "程序员",
                        WorkDays = 115,
                    });

                    attendanceSumList.Add(new AttendanceSum()
                    {
                        Name = "悦已坊",
                        Position = "技术助理",
                        WorkDays = 178,
                    });

                    attendanceSumList.Add(new AttendanceSum()
                    {
                        Name = "不一样的工控",
                        Position = "销售经理",
                        WorkDays = 125,
                    });


                    attendanceSumList.Add(new AttendanceSum()
                    {
                        Name = "XIAOA",
                        Position = "牛马",
                        WorkDays = 125,
                    });

                    attendanceSumList.Add(new AttendanceSum()
                    {
                        Name = "鱼乐",
                        Position = "架构师",
                        WorkDays = 185,
                    });
                }
                return attendanceSumList;
            }
            set
            {
                attendanceSumList = value;
                this.OnPropertyChanged();
            }
        }



        private DateTime dateTimeNow;

        public DateTime DateTimeNow
        {
            get { return dateTimeNow; }
            set
            {
                dateTimeNow = value;
                this.OnPropertyChanged();
            }
        }

    }
}
